package bTree.LeetCode;

import java.util.Arrays;

public class 分割等和子集 {
    public boolean canPartition(int[] nums) {
        int sum = Arrays.stream(nums).sum();
        if (sum % 2 == 1) return false;
        int half = sum / 2;
        int length = nums.length;
        boolean[] dp = new boolean[half + 1];
        dp[0] = true;
        for (int i = 0; i < length; i++) {
            for (int j = half; j >= 0; j--) {
                if (nums[i] <= j && dp[j - nums[i]]) {
                    dp[j] = true;
                }
            }
        }
        return dp[half];
    }
}